Skip to content

Conversation

@skuba31
Copy link

@skuba31 skuba31 commented Feb 5, 2026

Fixes crash when importing cherab.jet while SAL server is unreachable #40

Adds new subclass and function that check sal reachability only on instantiation or call so that the import of package is unaffectd.

Also adds a custom getattr method of cherab.jet.sal module that allows to use former notation from cherab.jet.sal import sal. Usage of this notation is discouraged as it results in the connection error being raised if SAL server is not reachable on import.

Comment on lines +15 to +22
def get_jet_sal(host: str = SAL_SERVER, **kwargs) -> SALClient:
"""Creates a SALClient instance connected to `host` if provided.
By default uses env variable `JET_SAL_SERVER` if set, `https://sal.jetdata.eu` otherwise.
"""
return SALClient(host=host, **kwargs)


def __getattr__(name: str):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless Github's formatting is off, I think you're missing some indentation here: these functions should be class methods.

Copy link
Author

@skuba31 skuba31 Feb 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the indentation is correct. I should have provided better explanation in the first description.

I wanted to create two ways for SAL creation, a function returning standard SAL class and a subclass that could potentially be expanded for other functionalities that jet.data.sal defines. I suppose one could be dropped in order to have one standardized way.

The custom getattr method should be on the module level to allow use of the current notation from cherab.jet.sal import sal introduced in #35 with the difference, that SAL server connection is checked only if this import is executed in code, not on package import itself.
This should ensure, that any scripts created with this notation remain working as long as SAL server is reachable.

The problem could be that custom getattr is available for python 3.7+. I am not sure what is the minimal python version cherab-jet aims to support.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see. That's a bit un-intuitive but makes sense. Essentially what you want is a lazy import, right? This could be done using importlib's LazyLoader (https://docs.python.org/3/library/importlib.html#implementing-lazy-imports): the intention may be a bit clearer to those reading the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants